if ( booleanExpression ){ one or more statements }else { one or more statements }
No. The indenting and line breaks are different, but logically it is the same as:
if ( booleanExpression ) { one or more statements } else { one or more statements }
Some programmers use the first style to "save" lines. This was once desirable when terminals could display only 23 lines. But the style leads to errors and extra debugging time.
Say that you are shopping at the Mall and find a $44.95 sweater you like, but (due to an impulsive cookie purchase) you might not have enough money to buy it. Here is a program that decides if you get the sweater:
import java.io.*; class SweaterPurchase { public static void main (String[] args) throws IOException { final int price = 4495; // price in cents BufferedReader stdin = new BufferedReader ( new InputStreamReader( System.in ) ); String inData; int cash; System.out.println("How much do you have, in pennies?"); inData = stdin.readLine(); cash = Integer.parseInt( inData ); if ( __________________ ) System.out.println("You can buy the sweater" ); else { System.out.println("You can't buy the sweater" ); System.out.println("You need $" + (price-cash)/100 + "." + (price-cash)%100 + " more." ); } } }
The reserved word final in the fifth line says that the value held in price will not be changed during the run of the program. This is not necessary, but if you use it, the compiler will check that you have not accidentally written a statement that tries to change the value. In long programs this is very useful.